package com.shengyuan.manage_os.old.ying.erpm;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.old.ying.erpm.data.Machinery;
import com.shengyuan.manage_os.old.ying.erpm.db.Actual_JiShi_Table;
import com.shengyuan.manage_os.old.ying.erpm.layout.GridAdapter_JX;
import com.shengyuan.manage_os.old.ying.erpm.layout.GridAdapter_SL;
import com.shengyuan.manage_os.old.ying.erpm.layout.PageIndicator;
import com.shengyuan.manage_os.old.ying.erpm.layout.Workspace;
import com.shengyuan.manage_os.old.ying.erpm.layout.Workspace.OnViewChangedListener;
import com.shengyuan.manage_os.old.ying.erpm.tasks.Submit_System_Setting_Data_Task;
import com.shengyuan.manage_os.old.ying.erpm.util.Utility;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.GridView;

public class WorkSpaceActivityGC extends Activity implements OnViewChangedListener {
	public static WorkSpaceActivityGC Instance;
	public static Machinery mMachinery=new Machinery();
	public static View view;
	
    protected Workspace mWorkspace;
    protected PageIndicator mIndicator;
    
    //public abstract Handler GetHandler();
    //public abstract List<Machinery>  LoadMenuListItems();
    public int mVerticalSpacing = 30;
    public static final int APP_PAGE_SIZE = 20;
    public boolean stop = false;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        stop = false;
        Instance = this;
        int k =10;
        if (getIntent().getExtras()!=null) {
            Object o = getIntent().getExtras().get("VerticalSpacing");
            if (o!=null) {
    			k = Integer.parseInt(o.toString());
    		}
		}

        setContentView(R.layout.ziyoujixie);

        mWorkspace = (Workspace) findViewById(R.id.workspace);
        mIndicator = (PageIndicator) findViewById(R.id.indicator);
        //List<ResolveInfo> apps = loadApps();
        List<Machinery> MachineryItems = LoadMenuListItems();																																																																																																																																																																		
//        Machinery machinery = new Machinery();
//        machinery.Name = "机械一号";
//        MachineryItems.add(machinery);
//        machinery = new Machinery();
//        machinery.Name = "机械二号";
//        MachineryItems.add( machinery);
        
        Actual_JiShi_Table dbo= new Actual_JiShi_Table(this);
        dbo.InitRows(MachineryItems);
        dbo.close();
        if (MachineryItems.size()>0) {
        	for (int i = 0; i < Math.ceil(1.0f * MachineryItems.size() / GridAdapter_JX.APP_PAGE_SIZE); i++) {
	            GridView grid = new GridView(this);
	            grid.setNumColumns(1);
	            grid.setHorizontalSpacing(5);
	            grid.setVerticalSpacing(k);
	            grid.setPadding(30, 20, 30, 20);
	            grid.setGravity(Gravity.CENTER);
	            grid.setAdapter(new GridAdapter_SL(this, MachineryItems, i));
	            mWorkspace.addView(grid);
	        }
	        mWorkspace.setOnViewChangedListener(this);
	        mIndicator.setIndication(mWorkspace.getChildCount(), 0);
		}else {
			Utility.MessageBox("请联系部门经理，添加台班车辆！");
			finish();
//			Intent intent = new Intent( this, Actual_Form_Car_Setting.class);
//			intent.putExtra("T_Name", Submit_System_Setting_Data_Task.T_WAIZUSHILUN);
//			intent.putExtra("mTitle", getResources().getString(R.string.title_actual_form_car_setting));
//			this.startActivity(intent);
		}
    }
    
	@Override
	protected void onDestroy() {
		stop = true;
		super.onDestroy();
	}
	@Override
	protected void onResume() {
    	//mWorkspace.refreshDrawableState();
    	mWorkspace.invalidate();
		super.onResume();
	}
	@Override
    public void onChange(int cnt, int index) {
        mIndicator.setIndication(cnt, index);
    }

	public List<Machinery> LoadMenuListItems()
	{
		String T_JIXIE="";
		try {
			T_JIXIE = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.
					getJSONObject(Submit_System_Setting_Data_Task.mID).getString("TaiBanCheLiang");
		} catch (JSONException e) {
			e.printStackTrace();
		}
			
		List<Machinery> list = new ArrayList<Machinery>();
//			String T_JIXIE="";
//			SharedPreferences sharedPrefrences = this.getSharedPreferences(SettingActivity.FILENAME, MODE_WORLD_READABLE);
//			T_JIXIE = sharedPrefrences.getString(Submit_System_Setting_Data_Task.T_WAIZUSHILUN, "");
		if (T_JIXIE!=null && T_JIXIE.trim().equals("")) {
			return list;
		}
		String[] array = T_JIXIE.split("\r\n");
		Machinery item;
    	
    	for (int i = 0; i < array.length; i++) {
    		if (!array[i].trim().equals("")) {
        		item = new Machinery();
    			item.Name = array[i];
    			list.add( item);
			}
		} 
    	return list;

	}
}